package com.taplinker.client.crm.rpc;

import android.widget.Toast;

import com.google.protobuf.InvalidProtocolBufferException;
import com.taplinker.client.AppConfigContext;
import com.taplinker.client.crm.domain.CrmDataCenter;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.dto.web.CrmCategoryProto;

/**
 * GetApp分类
 * 
 * @author djbian
 * 
 */
public class GetAppCategory extends SimpleBaseCommand {

	private String type;
	private int version;
	public static final String TYPE_APP = "app";
	public static final String TYPE_INDUSTRY = "industry";

	public GetAppCategory() {
	}

	public GetAppCategory(String type, int version) {
		this.type = type;
		this.version = version;
	}

	@Override
	public Result go() {
		return new HttpTemplate().exchange(AppConfigContext.TAPLINKER_WEB
				+ "/services/crm/v1/find/category/" + type + "/" + version);
	}

	@Override
	public void yes(Result result) {
		super.yes(result);
		try {
			CrmDataCenter.getInstance().setCrmCategory(
					CrmCategoryProto.CrmCategory
							.parseFrom((byte[]) result.object));
		} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
		}
		if (type.equals(TYPE_APP))
			CrmDataCenter.getInstance().noticeListener(
					CrmDataCenter.GET_APP_CATEGORY_APP);
		if (type.equals(TYPE_INDUSTRY))
			CrmDataCenter.getInstance().noticeListener(
					CrmDataCenter.GET_APP_CATEGORY_INDUSTRY);
	}

	@Override
	public void no(Result result) {
		Toast.makeText(CrmDataCenter.getInstance().getContext(),
				"" + result.message, Toast.LENGTH_SHORT).show();
	}

}